package nix.anim.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import nix.anim.common.pojo.NixResult;
import nix.anim.mapper.RightMapper;
import nix.anim.mapper.RoleMapper;
import nix.anim.mapper.RoleRightMapper;
import nix.anim.pojo.Right;
import nix.anim.pojo.Role;
import nix.anim.pojo.RoleRight;
import nix.anim.pojo.RoleRightExample;
import nix.anim.service.RoleRightService;

/**
 * 角色权限关系管理Service
 */
@Service
public class RoleRightServiceImpl implements RoleRightService {

	@Autowired
	private RoleRightMapper roleRightMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RightMapper rightMapper;
	
	@Override
	public NixResult deleteAll() {
		// 删除全部
		roleRightMapper.deleteByExample(new RoleRightExample());
		return NixResult.ok();
	}

	@Override
	public NixResult addRoleRight(Integer roleId, Integer rightId) {
		// 验证角色是否存在
		Role role = roleMapper.selectByPrimaryKey(roleId);
		if (role == null) {
			return NixResult.build(400, "关联的角色不存在！");
		}
		// 验证权限是否存在
		Right right = rightMapper.selectByPrimaryKey(rightId);
		if (right == null) {
			return NixResult.build(400, "关联的权限不存在！");
		}
		// 补全属性
		RoleRight roleRight = new RoleRight();
		roleRight.setRoleId(roleId);
		roleRight.setRightId(rightId);
		roleRight.setCreated(new Date());
		roleRight.setUpdated(new Date());
		// 插入数据
		roleRightMapper.insert(roleRight);
		return NixResult.ok();
	}

}
